Completed
Push — master ( 502c8a...95462b )
by Alejandro
14s
created

server.test.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 81
rs 8.4072
eloc 57

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { values } from 'ramda';
2
import reducer, {
3
  createServer,
4
  deleteServer,
5
  listServers,
6
  createServers,
7
  FETCH_SERVERS, FETCH_SERVERS_START,
8
} from '../../../src/servers/reducers/server';
9
10
describe('serverReducer', () => {
11
  const list = {
12
    abc123: { id: 'abc123' },
13
    def456: { id: 'def456' },
14
  };
15
  const expectedFetchServersResult = { type: FETCH_SERVERS, list };
16
  const ServersServiceMock = {
17
    listServers: jest.fn(() => list),
18
    createServer: jest.fn(),
19
    deleteServer: jest.fn(),
20
    createServers: jest.fn(),
21
  };
22
23
  describe('reducer', () => {
24
    it('returns servers when action is FETCH_SERVERS', () =>
25
      expect(reducer({}, { type: FETCH_SERVERS, list })).toEqual({ loading: false, list }));
26
  });
27
28
  describe('action creators', () => {
29
    beforeEach(() => {
30
      ServersServiceMock.listServers.mockClear();
31
      ServersServiceMock.createServer.mockReset();
32
      ServersServiceMock.deleteServer.mockReset();
33
      ServersServiceMock.createServers.mockReset();
34
    });
35
36
    describe('listServers', () => {
37
      const axios = { get: jest.fn().mockResolvedValue({ data: [] }) };
38
      const dispatch = jest.fn();
39
40
      beforeEach(() => {
41
        axios.get.mockClear();
42
        dispatch.mockReset();
43
      });
44
45
      it('fetches servers from local storage when found', async () => {
46
        await listServers(ServersServiceMock, axios)()(dispatch);
47
48
        expect(dispatch).toHaveBeenCalledTimes(2);
49
        expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START });
50
        expect(dispatch).toHaveBeenNthCalledWith(2, expectedFetchServersResult);
51
        expect(ServersServiceMock.listServers).toHaveBeenCalledTimes(1);
52
        expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
53
        expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
54
        expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
55
        expect(axios.get).not.toHaveBeenCalled();
56
      });
57
58
      it('tries to fetch servers from remote when not found locally', async () => {
59
        const NoListServersServiceMock = { ...ServersServiceMock, listServers: jest.fn(() => ({})) };
60
61
        await listServers(NoListServersServiceMock, axios)()(dispatch);
62
63
        expect(dispatch).toHaveBeenCalledTimes(2);
64
        expect(dispatch).toHaveBeenNthCalledWith(1, { type: FETCH_SERVERS_START });
65
        expect(dispatch).toHaveBeenNthCalledWith(2, { type: FETCH_SERVERS, list: {} });
66
        expect(NoListServersServiceMock.listServers).toHaveBeenCalledTimes(1);
67
        expect(NoListServersServiceMock.createServer).not.toHaveBeenCalled();
68
        expect(NoListServersServiceMock.deleteServer).not.toHaveBeenCalled();
69
        expect(NoListServersServiceMock.createServers).toHaveBeenCalledTimes(1);
70
        expect(axios.get).toHaveBeenCalledTimes(1);
71
      });
72
    });
73
74
    describe('createServer', () => {
75
      it('adds new server and then fetches servers again', () => {
76
        const serverToCreate = { id: 'abc123' };
77
        const result = createServer(ServersServiceMock, () => expectedFetchServersResult)(serverToCreate);
78
79
        expect(result).toEqual(expectedFetchServersResult);
80
        expect(ServersServiceMock.createServer).toHaveBeenCalledTimes(1);
81
        expect(ServersServiceMock.createServer).toHaveBeenCalledWith(serverToCreate);
82
        expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
83
        expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
84
        expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
85
      });
86
    });
87
88
    describe('deleteServer', () => {
89
      it('deletes a server and then fetches servers again', () => {
90
        const serverToDelete = { id: 'abc123' };
91
        const result = deleteServer(ServersServiceMock, () => expectedFetchServersResult)(serverToDelete);
92
93
        expect(result).toEqual(expectedFetchServersResult);
94
        expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
95
        expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
96
        expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
97
        expect(ServersServiceMock.deleteServer).toHaveBeenCalledTimes(1);
98
        expect(ServersServiceMock.deleteServer).toHaveBeenCalledWith(serverToDelete);
99
      });
100
    });
101
102
    describe('createServer', () => {
103
      it('creates multiple servers and then fetches servers again', () => {
104
        const serversToCreate = values(list);
105
        const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
106
107
        expect(result).toEqual(expectedFetchServersResult);
108
        expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
109
        expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
110
        expect(ServersServiceMock.createServers).toHaveBeenCalledTimes(1);
111
        expect(ServersServiceMock.createServers).toHaveBeenCalledWith(serversToCreate);
112
        expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
113
      });
114
    });
115
  });
116
});
117